class_name Weapon
extends Area2D

@onready var sprite = $Weapon
@onready var ani:AnimationPlayer = $Weapon/AnimationPlayer

var roting = false
var rot_speed:float = 0
var send_cha_b:bool = false


func _ready() -> void:
	pass

func rot_cha(delta):
	if send_cha_b:
		return
	if not ani.is_playing():
		if !roting:
			rot_speed = 0.0
		rot_speed+=200*delta
		roting = true
		sprite.rotation_degrees+= rot_speed*delta

func reset():
	send_cha_b = false
	roting = false
	rot_speed = 0
	show()
	get_parent().get_parent().re_send()

func send_cha():
	if send_cha_b:
		return
	send_cha_b = true
	var send_chaa = preload("res://scenes/Weapon/cha_weapon.tscn").instantiate()
	$SEND.add_child(send_chaa)
	send_chaa.global_transform = self.global_transform
	send_chaa.sprite.global_transform = self.sprite.global_transform
	send_chaa.rot_speed = self.rot_speed+20
	send_chaa.copy_pa =self
	if send_chaa.rot_speed>800:
		send_chaa.rot_speed = 800
	sprite.rotation_degrees = 90
	hide()
	

func _on_body_entered(body: Node2D) -> void:
	if send_cha_b:
		return
	if is_instance_valid(body) && body is Monster &&body.is_alive:
		$Weapon/AnimationPlayer.play("attack")
		body.die()


func _on_area_entered(area: Area2D) -> void:
	if send_cha_b:
		return
	if is_instance_valid(area) :
		var papa = area.get_parent().get_parent()
		if papa is Gem && papa.state == Gem.STATE.BUBBLIZE:
			$Weapon/AnimationPlayer.play("attack")
			papa.queue_free()
